package com.qfedu.mavenk.demo8;

import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc: 监听器,测试
 * @datatime: 2022/10/24 16:12
 */
//ContextEventHandler 监听器
//ContextStartedEvent 内置事件，context启动时触发
@Component
public class ContextEventHandler implements ApplicationListener<ContextStartedEvent> {

    //onApplicationEvent回调函数
    public void onApplicationEvent(ContextStartedEvent contextStartedEvent) {
        System.out.println("ApplicationContext 启动...");
    }

    public void onApplicationEvent(ContextStoppedEvent contextStoppedEvent) {
        System.out.println("ApplicationContext 停止...");
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //发送事件
        context.start();
        context.stop();
    }
}
